package com.banciyuan.bcywebview.base.helper;

import android.app.Activity;
import android.content.Context;
import android.os.Build;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;

/**
 * Created by hfxt2008 on 14-8-22.
 * 用于管理actionbar的管理类，但多次改版之后actionbar存在的页面已经很少了
 * 负责初始化actionbar的UI组件和管理监听事件
 */
public class ActionbarHelper implements OnClickListener {
    private Context mContext;
    private RelativeLayout mActionLayout;
    private ImageView mActionBarHome;
    private TextView mActionBarTitle;
    private ImageView mActionItemone;
    private ImageView mActionItemtwo;
    private TextView mActionBarHomeText;
    private TextView mActionBarItemoneText;

    private LinearLayout mTitleLayout;

    private OnActionCallbacks mCallbacks;

    public ActionbarHelper(Context mContext, View mActionBarView) {
        this(mContext, mActionBarView, 255);
    }

    public ActionbarHelper(final Context mContext, View mActionBarView, int alpha) {

        this.mContext = mContext;
        mActionLayout = (RelativeLayout) mActionBarView.findViewById(R.id.base_action_bar);
        mActionBarHome = (ImageView) mActionBarView.findViewById(R.id.base_action_bar_home);
        mActionBarTitle = (TextView) mActionBarView.findViewById(R.id.base_action_bar_title);
        mActionItemtwo = (ImageView) mActionBarView.findViewById(R.id.base_action_bar_itemtwo);
        mActionItemone = (ImageView) mActionBarView.findViewById(R.id.base_action_bar_itemone);
        mActionBarHomeText = (TextView) mActionBarView.findViewById(R.id.base_action_bar_home_text);
        mActionBarItemoneText = (TextView) mActionBarView.findViewById(R.id.base_action_bar_home_text_item);

        mTitleLayout = (LinearLayout) mActionBarView.findViewById(R.id.base_action_bar_title_lly);

        mActionBarHome.setOnClickListener(this);
        mTitleLayout.setOnClickListener(this);
        mActionBarHomeText.setOnClickListener(this);
        mActionBarItemoneText.setOnClickListener(this);
        mActionItemone.setOnClickListener(this);
        mActionItemtwo.setOnClickListener(this);
        setActionCallbacks(new OnActionCallbacks() {

            @Override
            public void onBackClick() {
                getContext().finish();
            }

            @Override
            public void onActionClick(int id) {
            }

        });
        setBgAlpha(alpha);
    }

    public Activity getContext() {
        return (Activity) mContext;
    }

    public void setTitle(CharSequence title) {
        if (title != null) mActionBarTitle.setText(title);
    }

    public void setTitle(int resId) {
        mActionBarTitle.setText(mContext.getResources().getString(resId));
    }

    public void setBgAlpha(int alpha) {
        mActionLayout.getBackground().setAlpha(alpha);
    }


    public void addOneAction(int resId) {
        mActionItemone.setVisibility(View.VISIBLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            mActionItemone.setImageDrawable(mContext.getResources().getDrawable(resId, mContext.getTheme()));
        } else {
            mActionItemone.setImageDrawable(mContext.getResources().getDrawable(resId));
        }
    }

    public void addBackText(String text) {
        mActionBarHomeText.setVisibility(View.VISIBLE);
        mActionBarHomeText.setText(text);
        mActionBarHome.setVisibility(View.GONE);
    }

    public void addItemOneText(String text) {
        mActionBarItemoneText.setVisibility(View.VISIBLE);
        mActionBarItemoneText.setText(text);
    }

    public void addItemOneText(String text, int color) {
        addItemOneText(text);
        mActionBarItemoneText.setTextColor(color);
    }

    public int getHeight() {
        return mActionLayout.getHeight();
    }

    /**
     * 设置action bar 回调
     *
     * @param callbacks
     */
    public void setActionCallbacks(OnActionCallbacks callbacks) {
        mCallbacks = callbacks;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_action_bar_home:
                callBack();
                return;
            case R.id.base_action_bar_home_text:
                callBack();
                return;

            case R.id.base_action_bar_itemone:
            case R.id.base_action_bar_itemtwo:
            case R.id.base_action_bar_title_lly:
            default:
                callAction(v.getId());
                return;
        }
    }

    private void callAction(int id) {
        if (mCallbacks != null) {
            mCallbacks.onActionClick(id);
        }
    }

    private void callBack() {
        if (mCallbacks != null) {
            mCallbacks.onBackClick();
        }
    }

    /**
     * action bar 回调
     *
     * @author hfxt2008
     */
    public interface OnActionCallbacks {

        /**
         * action bar中 返回和icon点击回调
         */
        void onBackClick();

        /**
         * action bar中 通过addAction增加的点击回调
         *
         * @param id
         */
        void onActionClick(int id);
    }
}
